﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace map.member
{
    public partial class ItemInfo : System.Web.UI.Page
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            //AliApi.appUserId = "8366652";
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AjaxFunction ));
        }

        protected void UpdateItemButton_ServerClick(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "alert", "<script type=\"text/javascript\">alert(1);</script>", false);
        }

       


    }


    #region　辅助类
    public class UploadFileTest1
    {
        private static string _fileType = ".zip|.rar|.txt";
        private static string _uploadPath = "UploadFiles/";
        private static int _maxFilesQuantity = 10;

        public static string FileType
        {
            get { return _fileType; }
            set { _fileType = value; }
        }

        public static string UploadPath
        {
            get { return _uploadPath + DateTime.Now.ToString("yyyy_MM") + "/"; }
            set { _uploadPath = value; }
        }

        public static int MaxFilesQuantity
        {
            get { return _maxFilesQuantity; }
            set { _maxFilesQuantity = value; }
        }


        /// <summary>
        /// Upload
        /// </summary>
        /// <param name="grade">CategoryId</param>
        /// <param name="gradeid">ItemId</param>
        [AjaxPro.AjaxMethod]
        public void Save()
        {
            HttpFileCollection files = HttpContext.Current.Request.Files;
            if (files.Count > 0 && files.Count <= _maxFilesQuantity)
            {
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    if (file.ContentLength > 0)
                    {
                        string fileName = GenerateFileName(System.IO.Path.GetFileName(file.FileName));
                        string uploadPath = UploadPath;
                        string filePath = HttpContext.Current.Server.MapPath("/" + uploadPath);

                        if (CheckExtensionName(fileName))
                        {
                            if (!System.IO.Directory.Exists(filePath)) System.IO.Directory.CreateDirectory(filePath);
                            file.SaveAs(filePath + fileName);

                            // Save to DB
                            //string id = files.AllKeys[i].Substring(5);
                            //AttachmentInfo item = new AttachmentInfo();
                            //item.Description = HttpContext.Current.Request.Form["filedesc_" + id].Trim();
                            //item.Url = uploadPath + fileName;
                            //item.Grade = grade;
                            //item.GradeID = gradeid;

                            //new AttachmentBLL().CreateAttachment(item);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Check the file's extension is valid or not.
        /// </summary>
        private static bool CheckExtensionName(string fileName)
        {
            if (FileType == ".*") return true;

            if (FileType.IndexOf(System.IO.Path.GetExtension(fileName)) == -1)
                return false;
            else
                return true;
        }

        /// <summary>
        /// Generate a new filename from the old filename.
        /// </summary>
        private static string GenerateFileName(string fileName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(new Random().Next(100, 999).ToString());
            sb.Append("_");
            sb.Append(DateTime.Now.ToString("HHmmss"));
            sb.Append("_");
            sb.Append(fileName);

            return sb.ToString();
        }
    }
    #endregion

}
